<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');


class Reg extends CI_Controller {

	function __construct()
	{
		parent::__construct();
	}

	public function index()
	{
		$this->load->helper('captcha');
		$vals = array(
    		'img_path' => './statics/captcha/',
			'img_url' => 'http://cireg.com/statics/captcha/',
		 	'font_path' => './system/fonts/CONSOLAI.TTF',	
			'img_width' => '200',
    		'img_height' => '35',

		);

		$cap = create_captcha($vals);
		$captchadata = array(
   		'captcha_time' => $cap['time'],
    	'ip_address' => $this->input->ip_address(),
   		 'word' => $cap['word']
		);
		$query = $this->db->insert_string('captcha', $captchadata);
		$this->db->query($query);
		//var_dump($cap);
		$data['captcha']=$cap['image'];
		$this->load->view('reg',$data);
	}
	public	function Error($msg) {
		echo("
   				<Script>
   				window.alert(\"$msg\")
   				history.go(-1)
   				</Script>"); 
		exit;

	}
	public function add(){
		echo '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">';

		$expiration = time()-7200; // 2小时限制
		$this->db->query("DELETE FROM captcha WHERE captcha_time < ".$expiration);

		// 然后再看是否有验证码存在:
		$sql = "SELECT COUNT(*) AS count FROM captcha WHERE word = ? AND ip_address = ? AND captcha_time > ?";
		$binds = array($_POST['captcha'], $this->input->ip_address(), $expiration);
		$query = $this->db->query($sql, $binds);
		$row = $query->row();

		if ($row->count == 0)
		{
			$this->Error('验证码错误！');
		}
		
		
		$this->load->helper('url');
		$this->load->helper('date');
		$this->load->model('user_m');
		$this->load->model('event_log_m');
		$data=array(
			'username'=>$_POST['username'],
			'password'=>md5($_POST['password']),
			'email'=>$_POST['email'],
			'tel'=>$_POST['tel'],
			'address'=>$_POST['address'],
			'time'=>unix_to_human(now(), TRUE,'PRC')
		);
		$event=array(
			'username'=>$_POST['username'],
			'event'=>'register',
			'time'=>unix_to_human(now(), TRUE,'PRC')
		);
		$preg_username='/^([\x{4e00}-\x{9fa5}]{2,5}|\w{4,10})/u';
		$preg_password='/^[\w]{8,15}$/u';
		$preg_email='/^(\w)+@(\w+)\.(\w+)$/u';
		$preg_tel='/^(\d){11}$/u';
		$preg_address='/([\x{4e00}-\x{9fa5}]|[\w]){10,50}/u';


		if($this->user_m->user_SelectByUserName($_POST['username'])!=null){
			$this->Error('已注册过用户名！');
		}
		if(preg_match($preg_username,$_POST['username'],$tmp)==0){
			$this->Error('请输入合法的用户名！');
		}
		if(preg_match($preg_password,$_POST['password'],$tmp)==0){
			$this->Error('请输入合法的密码！');
		}
		if($_POST['password']!=$_POST['password_re']){
			$this->Error('两次输入密码不一致！');
		}
		if(preg_match($preg_tel,$_POST['tel'],$tmp)==0){
			$this->Error('请输入合法的中国大陆电话！');
		}
		if(preg_match($preg_email,$_POST['email'],$tmp)==0){
			$this->Error('请输入合法的邮箱！');
		}

		if(preg_match($preg_address,$_POST['address'],$tmp)==0){
			$this->Error('请输入详细的地址！');
		}

		$this->user_m->user_insert($data);
		$this->event_log_m->user_insert($event);
		echo "<script language='javascript' type='text/javascript'>";
		echo 'alert("注册成功,请先激活帐号！");';
		echo "</script>";
		redirect('act','refresh');
	}
}
?>